11. Update Pet
Update Pet
Question:
Editing Pets
There’s one major thing missing from our Edit Pet version of the page, and it is the ability to actually, well, edit the pet
.
At the moment, if you change any of the values on an existing pet, it doesn’t update the pet. What it does do is save a copy of the pet…which is not what we want.
The solution is for the “edit pet” mode to update the current pet, and for the “add pet mode” to insert a new pet.
Instead of having an “insertPet” method, let’s change or “refactor” its name to “savePet”. And in here, you can figure out, as we did above, whether the EditorActivity is in “insert” mode of “edit mode”. You’ll want to perform an “update” operation using the content resolver if it’s in edit mode.
Alright, give it a try, update the save pet method. Once you’re done, instead of creating a copy of the pet, the edit activity should update the existing pet. When you press the fab button to insert a new pet, it should do that correctly as well. In both cases your app should show toast messages.
Start Quiz:
Solution:
Regardless of whether you are updating or creating a pet for the first time, you’ll need a content values object. It’s created the same way it was before. Once it’s created, you need to act differently depending on whether you’re editing an existing pet, or inserting a new pet.
You can differentiate between edit mode and insert mode the same way you did to set the title, by checking whether mCurrentPetUri is null or not. If it is null, then this is a new pet, and you’ll run the code that was originally in this method.
if (mCurrentPetUri == null) {
If the uri is not null, though, this means that you are updating an existing pet. Using the ContentResolver’s update method, you’ll update the pet. The URI of what you’re updating is mCurrentPetUri, which makes writing this code easy.
} else {
// Otherwise this is an EXISTING pet, so update the pet with content URI: mCurrentPetUri
// and pass in the new ContentValues. Pass in null for the selection and selection args
// because mCurrentPetUri will already identify the correct row in the database that
// we want to modify.
int rowsAffected = getContentResolver().update(mCurrentPetUri, values, null, null);
Now this returns a number of updated rows. To display whether the update was successful or not, we can check the number of updated rows. If it was 0, then the update was not successful, and we can show an error toast. Otherwise we show a success toast.
// Show a toast message depending on whether or not the update was successful.
if (rowsAffected == 0) {
// If no rows were affected, then there was an error with the update.
Toast.makeText(this, getString(R.string.editor_update_pet_failed),
Toast.LENGTH_SHORT).show();
} else {
// Otherwise, the update was successful and we can display a toast.
Toast.makeText(this, getString(R.string.editor_update_pet_successful),
Toast.LENGTH_SHORT).show();
}
INSTRUCTOR NOTE:
Hint: How can you tell if we’re in the “insert new pet” case or “edit existing pet” case? You did this check when setting the EditorActivity title.
Note: Be sure to declare your user-visible strings in the strings.xml file for the toast messages
See ContentResolver update() example here